<?php

namespace addon\lian\shop\controller;

use addon\lian\model\Config as ConfigModel;
use addon\lian\model\LianChange;
use addon\lian\model\LianMember;
use addon\lian\model\LianLevel as LianLevelModel;
use app\model\goods\Goods as GoodsModel;
use app\model\member\Member as MemberModel;
use app\shop\controller\BaseShop;
use think\App;
use think\facade\Log;
use think\facade\Db;

/**
 *  会员设置
 */
class Change extends BaseShop
{
    public function __construct(App $app = null)
    {
        $this->replace = [
            'LIAN_JS' => __ROOT__ . '/addon/lian/shop/view/public/js',
            'LIAN_CSS' => __ROOT__ . '/addon/lian/shop/view/public/css'
        ];
        parent::__construct($app);
    }


    /**
     * 链动变动记录列表
     */
    public function lists()
    {
        $model = new LianChange();
        if (request()->isJson()) {

            $nickname = input('nickname', '');
            $mobile = input('mobile', '');
            $level_id = input('level_id', '');
            $start_time = input('start_time', ''); 
            $end_time = input('end_time', '');


            $page = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);

            
            // 使用模型关联查询 - 不使用表别名
            $condition = [];
            $condition[] = [ 'site_id', '=', $this->site_id ];

            if($nickname){
                $condition[] = [ 'member.nickname', 'like', '%' . $nickname . '%' ];
            }
            if($mobile){
                $condition[] = [ 'member.mobile', 'like', '%' . $mobile . '%' ];
            }

            if ($start_time && $end_time) {
                $condition[] = [ 'create_time', 'between', [ date_to_time($start_time), date_to_time($end_time) ] ];
            } elseif (!$start_time && $end_time) {
                $condition[] = [ 'create_time', '<=', date_to_time($end_time) ];
            } elseif ($start_time && !$end_time) {
                $condition[] = [ 'create_time', '>=', date_to_time($start_time) ];
            }

            $list = (new LianChange)->getPage($this->site_id, $condition, $page, $page_size);
            
            return $list;

        } else {
            $level_model = new LianLevelModel();
            $level_list = $level_model->getLevelList([ [ 'status', '=', 1 ], [ 'site_id', '=', $this->site_id ] ], 'level_id,level_name,level_num')[ 'data' ];
            $this->assign('level_list', $level_list);

            $config_model = new ConfigModel();
            $basics = $config_model->getLianBasicsConfig($this->site_id)[ 'data' ][ 'value' ];
            $this->assign('basics_info', $basics);

            return $this->fetch('change/lists');
        }
    }

    /**
     * 添加链动会员
     */
    public function add()
    {
        if (request()->isJson()) {

            $lian_data = [
                'site_id' => $this->site_id,
                'fenxiao_name' => input('fenxiao_name', ''),//分销商名称
                'level_id' => input('level_id', 0),//分销商等级
                'parent' => input('fenxiao_id', 0),//上级分销商ID
                'member_id' => input('member_id', 0),//关联会员ID
                'pid' => input('pid', 0),
                'lian_pid' => input('lian_pid', 0),
                'status' => input('status', 0),
                'thaw_status' => input('thaw_status', 0),
                'up_time' => input('up_time', 0),
            ];
            $model = new LianMember();
            return $model->addLianMember($lian_data);

        } else {

            //获取链动等级
            $level_model = new LianLevelModel();
            $level_list = $level_model->getLevelList([ [ 'status', '=', 1 ], [ 'site_id', '=', $this->site_id ] ], 'level_id,level_name,level_num')[ 'data' ];
            $this->assign('level_list', $level_list);

            //获取会员列表
            $member_model = new MemberModel();
            $where[] = [ 'site_id', '=', $this->site_id ];
            $member_list = $member_model->getMemberList($where, 'member_id,nickname')[ 'data' ];
            $this->assign('member_list', $member_list);

            return $this->fetch('member/add');
        }

    }


    /**
     * 获取会员列表
     */
    public function getMemberList()
    {
        $page_index = input('page', 1);
        $page_size = input('page_size', PAGE_LIST_ROWS);
        $member_search = input('member_search', '');
        $condition = [];
        $condition[] = [ 'mobile|email|username|nickname', 'like', '%' . $member_search . '%' ];
        $condition[] = [ 'site_id', '=', $this->site_id ];
        $member_model = new MemberModel();
        $list = $member_model->getMemberPageList($condition, $page_index, $page_size, '', 'member_id,headimg,nickname,point,balance');
        return $list;
    }

    /**
     * 详情
     */
    public function detail()
    {
        $fenxiao_id = input('fenxiao_id', '');

        $model = new FenxiaoModel();
        $fenxiao_leve_model = new FenxiaoLevelModel();

        $condition[] = [ 'f.fenxiao_id', '=', $fenxiao_id ];
        $info = $model->getFenxiaoDetailInfo($condition);
        if (empty($info[ 'data' ])) $this->error('未获取到分销商数据', href_url('fenxiao://shop/fenxiao/lists'));
        //团队人员数据
        $info['data']['team_num'] = $model->getFenxiaoTeamNum($fenxiao_id, $this->site_id)['data'];

        $fenxiao_level = $fenxiao_leve_model->getLevelInfo([ [ 'level_id', '=', $info[ 'data' ][ 'level_id' ] ] ]);

        $this->assign('status', $model->fenxiao_status_zh);
        $this->assign('level', $fenxiao_level[ 'data' ]);
        $this->assign('info', $info[ 'data' ]);

        $this->assign('fenxiao_id', $fenxiao_id);

        $config_model = new ConfigModel();
        $basics_config = $config_model->getFenxiaoBasicsConfig($this->site_id);
        $this->assign('fenxiao_level_num', $basics_config[ 'data' ][ 'value' ][ 'level' ]);

        return $this->fetch('fenxiao/fenxiao_detail');
    }


    /**
     * 冻结
     */
    public function frozen()
    {
        $member_id = input('member_id', '');
        $model = new LianMember();
        return $model->frozen($member_id);
    }

    /**
     * 恢复正常
     */
    public function unfrozen()
    {
        $member_id = input('member_id', '');
        $model = new LianMember();
        return $model->unfrozen($member_id);
    }

    


    /**
     * 变更上下级
     */
    public function change()
    {
        $member_id = input('member_id');
        $model = new FenxiaoModel();
        $member_model = new MemberModel();

        //用户信息
        $member_info = $member_model->getMemberInfo([ [ 'member_id', '=', $member_id ] ], 'fenxiao_id,is_fenxiao')[ 'data' ];
        //获取分销信息
        $fenxiao_info = $model->getFenxiaoInfo([ [ 'fenxiao_id', '=', $member_info[ 'fenxiao_id' ] ] ], 'parent');
        //获取上级分销商信息
        $parent_info = [];
        if (!empty($fenxiao_info[ 'data' ])) {
            $parent_info = $model->getFenxiaoInfo([ [ 'fenxiao_id', '=', $fenxiao_info[ 'data' ][ 'parent' ] ] ], 'fenxiao_id,fenxiao_name')['data'];
        }
        if (request()->isJson()) {
            if ($member_info[ 'is_fenxiao' ] == 1) {
                $condition[] = [ 'f.fenxiao_id', '<>', $member_info[ 'fenxiao_id' ] ];
                $condition[] = [ 'f.parent', '<>', $member_info[ 'fenxiao_id' ] ];
            }

            $condition[] = [ 'f.site_id', '=', $this->site_id ];
            $status = input('status', 1);
            if ($status) {
                $condition[] = [ 'f.status', '=', $status ];
            }

            $fenxiao_name = input('fenxiao_name', '');
            if ($fenxiao_name) {
                $condition[] = [ 'f.fenxiao_name', 'like', '%' . $fenxiao_name . '%' ];
            }

            $parent_name = input('parent_name', '');
            if ($parent_name) {
                $condition[] = [ 'pf.fenxiao_name', 'like', '%' . $parent_name . '%' ];
            }

            $level_id = input('level_id', '');
            if ($level_id) {
                $condition[] = [ 'f.level_id', '=', $level_id ];
            }
            $start_time = input('start_time', '');
            $end_time = input('end_time', '');
            if ($start_time && $end_time) {
                $condition[] = [ 'f.create_time', 'between', [ date_to_time($start_time), date_to_time($end_time) ] ];
            } elseif (!$start_time && $end_time) {
                $condition[] = [ 'f.create_time', '<=', date_to_time($end_time) ];

            } elseif ($start_time && !$end_time) {
                $condition[] = [ 'f.create_time', '>=', date_to_time($start_time) ];
            }
            if(!empty($parent_info)){
                $order = \think\facade\Db::raw("IF(f.fenxiao_id = {$parent_info['fenxiao_id']}, 1, 2) asc,f.create_time desc");
            }else{
                $order = 'f.create_time desc';
            }

            $page = input('page', 1);
            $page_size = input('page_size', PAGE_LIST_ROWS);
            $list = $model->getFenxiaoPageList($condition, $page, $page_size, $order);
            return $list;

        } else {
            $level_model = new FenxiaoLevelModel();
            $level_list = $level_model->getLevelList([ [ 'status', '=', 1 ], [ 'site_id', '=', $this->site_id ] ], 'level_id,level_name');
            $this->assign('level_list', $level_list[ 'data' ]);

            $config_model = new ConfigModel();
            $basics = $config_model->getFenxiaoBasicsConfig($this->site_id);
            $this->assign('basics_info', $basics[ 'data' ][ 'value' ]);

            $this->assign('member_id', $member_id);
            $this->assign('parent_info', $parent_info);

            $change_end_func = input('change_end_func', 'changeEnd');
            $this->assign('change_end_func', $change_end_func);

            return $this->fetch('fenxiao/change');
        }
    }

    /**
     * 确认变更
     */
    public function confirmChange()
    {
        if (request()->isJson()) {

            $member_id = input('member_id', '');
            $parent = input('parent', '');
            $type = input('type', '');
            $model = new FenxiaoModel();
            if ($type == 1) {
                $res = $model->changeParentFenxiao($member_id, $parent);
            } else {
                $res = $model->cancelParentFenxiao($member_id);
            }

            return $res;
        }
    }

    /**
     * 会员详情
     */
    public function memberInfo()
    {
        if (request()->isJson()) {

            $member_id = input('member_id', '');
            $condition[] = [ 'site_id', '=', $this->site_id ];
            $condition[] = [ 'member_id', '=', $member_id ];
            $member_model = new MemberModel();
            $member_info_result = $member_model->getMemberInfo($condition);
            $member_info = $member_info_result['data'];
            if (empty($member_info)) return $member_model->error([], '账号不存在！');
            return $member_info_result;
        }
    }

    /**
     * 修改链动会员等级
     */
    public function changeLevel()
    {
        $member_id = input('member_id', '');
        $lian_id = input('lian_id', '');
        if (request()->isJson()) {
            $level_model = new LianLevelModel();
            $lian_model = new LianMember();
            $lian_info = $lian_model->getLianMemberInfo([ [ 'lian_id', '=', $lian_id ] ], 'member_id,level_id,level_name')[ 'data' ];
            $condition[] = [ 'site_id', '=', $this->site_id ];
            if (!empty($lian_info)) {
                $condition[] = [ 'level_id', '<>', $lian_info[ 'level_id' ] ];
            }

            $lists = $level_model->getLevelPageList($condition, '1', PAGE_LIST_ROWS, 'level_num asc');
            return $lists;
        } else {
            $config_model = new ConfigModel();
            $basics = $config_model->getLianBasicsConfig($this->site_id);
            $this->assign('basics_info', $basics[ 'data' ][ 'value' ]);

            $this->assign('member_id', $member_id);

            $this->assign('lian_id', $lian_id);

            $change_end_func = input('change_end_func', 'changeEnd');
            $this->assign('change_end_func', $change_end_func);

            return $this->fetch('member/change_level');
        }

    }

    /**
     * 确认变更
     */
    public function confirmChangeLevel()
    {
        if (request()->isJson()) {

            $member_id = input('member_id', '');
            $level_id = input('level_id', '');

            $level_model = new LianLevelModel();
            $level_data = $level_model->getLevelInfo([ [ 'level_id', '=', $level_id ] ], 'level_id,level_name')[ 'data' ];
            $lian_model = new LianMember();
            $data = [
                'level_id' => $level_data[ 'level_id' ],
                'level_name' => $level_data[ 'level_name' ]
            ];
            $res = $lian_model->changeLianMemberLevel($data, [ [ 'member_id', '=', $member_id ] ]);
            return $res;
        }
    }
}